package com.woniu.microcommunityserver.book.mapper;

import com.woniu.microcommunityserver.book.entity.BookInformationInfoEntity;
import com.woniu.microcommunityserver.book.entity.BorrowRevertInformationInfoEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface BorrowRevertInformationInfoMapper {
    /**
     * 查询所有借阅记录
     * @return
     */
    @Select("select id,fk_book_id,user_name,user_village,user_room,user_phone," +
            "borrow_time,revert_time,borrow_revert_state," +
            "borrow_revert_remark from borrow_revert_information_info")
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="fk_book_id",property="fkBookId"),
            @Result(column="user_name",property="userName"),
            @Result(column="user_village",property="userVillage"),
            @Result(column="user_room",property="userRoom"),
            @Result(column="user_phone",property="userPhone"),
            @Result(column="borrow_time",property="borrowTime"),
            @Result(column="revert_time",property="revertTime"),
            @Result(column="borrow_revert_state",property="borrowRevertState"),
            @Result(column="borrow_revert_remark",property="borrowRevertRemark"),
            @Result(column="fk_book_id",property="bookInformationInfoEntity",javaType = BookInformationInfoEntity.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.book.mapper.BookInformationInfoMapper.findOne",fetchType = FetchType.EAGER
                    ))
    })
    List<BorrowRevertInformationInfoEntity> findAll();

    /**
     * 查询指定借阅记录
     * @param map
     * @return
     */
    @Select({"<script> " +
            "select id,fk_book_id,user_name,user_village,user_room,user_phone," +
            "borrow_time,revert_time,borrow_revert_state," +
            "borrow_revert_remark from borrow_revert_information_info" +
            "<where>" +
            "<if test='userName!= null'>" +
            "and user_name like concat('%', #{userName}, '%')"+
            "</if>"+
            "<if test='userPhone!= null'>" +
            "and user_phone = #{userPhone}"+
            "</if>"+
            "<if test='borrowRevertState!= null'>" +
            "and borrow_revert_state = #{borrowRevertState}"+
            "</if>"+
            "</where>"+
            "</script>"})
    @Results({
            @Result(id=true,column="id",property="id"),
            @Result(column="fk_book_id",property="fkBookId"),
            @Result(column="user_name",property="userName"),
            @Result(column="user_village",property="userVillage"),
            @Result(column="user_room",property="userRoom"),
            @Result(column="user_phone",property="userPhone"),
            @Result(column="borrow_time",property="borrowTime"),
            @Result(column="revert_time",property="revertTime"),
            @Result(column="borrow_revert_state",property="borrowRevertState"),
            @Result(column="borrow_revert_remark",property="borrowRevertRemark"),
            @Result(column="fk_book_id",property="bookInformationInfoEntity",javaType = BookInformationInfoEntity.class,
                    one=@One(
                            select="com.woniu.microcommunityserver.book.mapper.BookInformationInfoMapper.findOne",fetchType = FetchType.EAGER
                    ))
    })
    List<BorrowRevertInformationInfoEntity> findAny(Map<String,Object> map);

    /**
     * 新增借阅记录
     * @param borrowRevertInformationInfoEntity
     * @return
     */
    @Insert("insert into borrow_revert_information_info(fk_book_id,user_name,user_village,user_room," +
            "user_phone,borrow_time,borrow_revert_state,borrow_revert_remark) values(#{b.fkBookId}," +
            "#{b.userName},#{b.userVillage},#{b.userRoom},#{b.userPhone}," +
            "#{b.borrowTime},#{b.borrowRevertState},#{b.borrowRevertRemark})")
    boolean insertBorrow(@Param("b") BorrowRevertInformationInfoEntity borrowRevertInformationInfoEntity);

    /**
     * 归还图书修改借阅记录状态
     * @param id
     * @return
     */
    @Update("update borrow_revert_information_info set borrow_revert_state=1 ,revert_time=#{revertTime} where id=#{id}")
    boolean updateState(Long id,String revertTime);
}
